package com.fatjay.gettop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class top extends ListActivity {
	Map<String, String> threadlist = new HashMap<String, String>();				//key: name of thread, value:url$auther$autherInfoURL
	TopAdapter mAdapter = new TopAdapter(this);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//ListAdapter mAdapter = new TopAdapter(this);
		getTop();
		setListAdapter(mAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		System.out.println("clicked!");
		String url = threadlist.get( ((TopAdapter)mAdapter).groups.get(position) );
		Intent startread = new Intent();
		Bundle mBundle = new Bundle();
		mBundle.putString("url", url);
		startread.putExtras(mBundle);
		startread.setClass(top.this, threadContent.class);
		top.this.startActivity(startread);
	}
	
	private String getTop() {
    	String myString = null;
    	//StringBuffer sff = new StringBuffer();
    	try {
    		Document doc = Jsoup.connect("http://bbs.nju.edu.cn/bbstop10").get();
    		Elements blocks = doc.select("tr");
    		for (Element block : blocks) {
    			Elements links = block.select("a[href]");
    			if (links.size()==0)
    				continue;
    			String title = links.get(1).text();
    			String url = links.get(1).attr("abs:href");
    			String username = links.get(2).text();
    			String userinfo = links.get(2).attr("abs:href");
    			String board = links.get(0).text();
    			String boardurl = links.get(0).attr("abs:href");
    			mAdapter.groups.add(title);
    			threadlist.put(title, url+'#'+username+'#'+userinfo+'#'+board+'#'+boardurl);
    			//sff.append(link.attr("abs:href")).append("  ").append(link.text()).append("\n");
    		}
    	} catch (Exception e) {
    		myString = e.getMessage();
    		e.printStackTrace();
    	}
    	return myString;
    }
	
	private class TopAdapter extends BaseAdapter {

		public ArrayList<String> groups = new ArrayList<String>();
		private Context mContext;
		
		TopAdapter(Context con) {
			mContext = con;
		}
		
		public int getCount() {
			// TODO Auto-generated method stub
			return groups.size();
		}

		@Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            return !groups.get(position).startsWith("-");
        }
		
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv;
            if (convertView == null) {
                tv = (TextView) LayoutInflater.from(mContext).inflate(
                        android.R.layout.simple_expandable_list_item_1, parent, false);
            } else {
                tv = (TextView) convertView;
            }
            tv.setText(groups.get(position));
            return tv;
		}
		
	}
	
}
